package com.rivercloud.dao.Impl;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.rivercloud.dao.UserDao;
import com.rivercloud.entity.Project;
import com.rivercloud.entity.Role;
import com.rivercloud.entity.User;
import com.rivercloud.entity.UserProject;

public class UserDaoImpl implements UserDao {
	 static SqlSessionFactory sqlSessionFactory = null;
	 static{
		 SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
		 InputStream inputStream =null;
		 
		 try {
			inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
	 }   

	@Override
	public int findIdByName(String userName) {
		SqlSession session = sqlSessionFactory.openSession();
		User user = session.selectOne("findIdByUserName",userName);
		session.close();
		return user.getId();
	}


	@Override
	public void addUser(User user) {
		SqlSession session = sqlSessionFactory.openSession();
		session.insert("regist", user);
		session.commit();
		session.close();
		
		
	}


	@Override
	public User userLogin(User user) {
		SqlSession session = sqlSessionFactory.openSession();
		User existUser = session.selectOne("userLogin", user);
		session.close();
		return existUser;
	}


	@Override
	public List<User> findAllUser() {
		SqlSession session = sqlSessionFactory.openSession();
		List<User> users = session.selectList("findAllUser");
		session.close();
		return users;
	}


	@Override
	public List<User> findUserByIds(List<Integer> list) {
		SqlSession session = sqlSessionFactory.openSession();
		List<User> users = session.selectList("findUserByIds",list);
		session.close();
		return users;
	}


	@Override
	public User checkUserName(String userName) {
		SqlSession session = sqlSessionFactory.openSession();
		User user = session.selectOne("checkUserName",userName);
		session.close();
		return user;
		
	}


	@Override
	public List<User> query_user_role_permission_all1(Integer uid) {
		// TODO Auto-generated method stub
		SqlSession session = sqlSessionFactory.openSession();
		List<User> users = session.selectList("query_user_role_permission_all1",uid);
		return users;
	}

	
	

	@Override
	public List<User> query_user_role_permission_all2(UserProject userProject) {
		// TODO Auto-generated method stub
				SqlSession session = sqlSessionFactory.openSession();
				List<User> users = session.selectList("query_user_role_permission_all2",userProject);
				return users;
	}


	@Override
	public User findUserById(int userId) {
		SqlSession session = sqlSessionFactory.openSession();
		User user = session.selectOne("findUserById",userId);
		session.close();
		return user;
	}


	@Override
	public void changePassword(User user) {
		SqlSession session = sqlSessionFactory.openSession();
		session.insert("changePassword", user);
		session.commit();
		session.close();
	}
	
	
	
}
